package com.hs.framework.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.annotation.Nullable
import com.hs.framework.databinding.ViewPaginationBinding
import com.hs.framework.databinding.ViewSharedheaderBinding

/**
 * 共享头部
 */
class SharedHeaderView : LinearLayout {

    private  var  mbinding : ViewSharedheaderBinding? = null

    constructor(context: Context) : super(context) {
        init(context, null)
    }

    constructor(context: Context, @Nullable attrs: AttributeSet?) : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init(context, attrs)
    }

    private fun init(context: Context, @Nullable attrs: AttributeSet?) {
        mbinding = ViewSharedheaderBinding.inflate(LayoutInflater.from(context),this,true)
        mbinding!!.hearderBackLayout.setOnClickListener(object : OnClickListener{
            override fun onClick(p0: View?) {
                if (callBack != null)callBack!!.onFinish()
            }

        })
    }

    /**
     * 设置标题
     */
    public fun setHeaderTitle(title:String?){
        mbinding?.headerTitle?.visibility = VISIBLE
        mbinding?.headerTitle?.text = title?:""
    }


    /**
     * 设置左边图标
     */
    public fun setHeaderLeftImage(imageResource : Int){
        mbinding?.headerLeftImage?.visibility = VISIBLE
        mbinding?.headerLeftImage?.setImageResource(imageResource)
    }

    /**
     *  设置倒计时 时间
     */
    public  fun setHerderCountdown(count : Int = 60){
        mbinding?.herderCountdownTv?.visibility = VISIBLE
        mbinding?.herderCountdownTv?.text = "${count}S"
    }

    /**
     *  设置右边图表
     */
    public fun setHeaderRightImage(imageResource : Int){
        mbinding?.headerLeftImage?.visibility = VISIBLE
        mbinding?.headerLeftImage?.setImageResource(imageResource)
    }


    fun onDestroyView(){
        mbinding = null
    }


    private var callBack:CallBack?= null

    fun setCallBack(callBack:CallBack){
        this.callBack = callBack
    }

    public  interface CallBack{
       fun onFinish()
    }
}